<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="dateRange"
        value-format="yyyy-MM-dd"
        type="daterange"
        range-separator="~"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :editable="false"
        class="filter-item"
        @change="changeDate"
      />
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="日期" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.runDate | parseTime('{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="运营结束时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.finishTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.updateTime | parseTime('{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作用户" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.updateUser }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center">
        <template slot-scope="{ row }">
          <span>{{ row.memo }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="type == 0" label="操作" align="center" class-name="small-padding fixed-width" width="100">
        <template slot-scope="{ row }">
          <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(row)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />
    <div v-if="dialogEditFormVisible">
      <el-drawer :visible.sync="dialogEditFormVisible" direction="rtl" size="85%" :append-to-body="true" :destroy-on-close="true" :wrapper-closable="false" :with-header="false">
        <FinishsettingAddForm :id="editId" ref="finishsettingEditForm" @cancelClose="cancelClose(arguments)" />
      </el-drawer>
    </div>
  </div>
</template>

<script>
import { getSysFinishSetting } from '@/api/finishsetting';
import waves from '@/directive/waves';
import Pagination from '@/components/Pagination';
import FinishsettingAddForm from '@/views/sys/finishsettingAdd.vue';
import { parseTime } from '@/utils';

export default {
  name: 'SysFinishSetting',
  directives: { waves },
  components: { Pagination, FinishsettingAddForm },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        pageNum: 1,
        pageSize: 10,
        beginTime: '',
        endTime: ''
      },
      dateRange: [],
      dialogEditFormVisible: false,
      editId: 0,
      type: null
    };
  },
  created() {
    const { params } = this.$route
    Object.keys(params).forEach(key => {
      this.type = params[key].split(':')[1];
    });
    const nowDate = new Date();
    const nextDate = nowDate.setDate(nowDate.getDate() + 30);
    this.dateRange.push(parseTime(new Date(), '{y}-{m}-{d}'));
    this.dateRange.push(parseTime(nextDate, '{y}-{m}-{d}'));
    this.listQuery.beginTime = this.dateRange[0];
    this.listQuery.endTime = this.dateRange[1];
    this.getList();
  },
  methods: {
    async getList() {
      this.listLoading = true;
      await getSysFinishSetting(this.listQuery).then(response => {
        console.log(response.data)
        this.list = response.data.list;
        this.total = response.data.total;
        this.listLoading = false;
      });
    },
    async handleSearch() {
      this.getList();
    },
    changeDate(val) {
      if (val) {
        this.listQuery.beginTime = val[0];
        this.listQuery.endTime = val[1];
      } else {
        this.listQuery.beginTime = null;
        this.listQuery.endTime = null;
      }
    },
    handleEdit(row) {
      this.editId = row.id;
      this.dialogEditFormVisible = true;
    },
    cancelClose(data) {
      this.dialogEditFormVisible = false;
      if (data[0]) {
        this.getList();
      }
    }
  }
};
</script>
